package com.example.projectmobile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class PlaceDataSource {
	private MySQLiteHelper dbHelper;
	private SQLiteDatabase db;
	private String getPlaceNumber = "SELECT MAX(id) FROM "
			+ MySQLiteHelper.TABLE_PLACE;
	private String[] allRecordColumns = { MySQLiteHelper.COLUMN_NAME };
	private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
			MySQLiteHelper.COLUMN_NAME,MySQLiteHelper.COLUMN_DESCRIPTION, MySQLiteHelper.COLUMN_LAT,
			MySQLiteHelper.COLUMN_LNG, MySQLiteHelper.COLUMN_PIC };
	private String[] idColum = { MySQLiteHelper.COLUMN_ID };

	public PlaceDataSource(Context context) {
		dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		db = dbHelper.getWritableDatabase();

	}

	public void close() {

		dbHelper.close();

	}

	public Place createRecord(String name, String des , double lat,double lon, String pic) {

		ContentValues values = new ContentValues();

		values.put(MySQLiteHelper.COLUMN_NAME, name);
		values.put(MySQLiteHelper.COLUMN_DESCRIPTION, des);
		values.put(MySQLiteHelper.COLUMN_LAT, lat);
		values.put(MySQLiteHelper.COLUMN_LNG, lon);
		values.put(MySQLiteHelper.COLUMN_PIC, pic);

		long insertId = db.insert(MySQLiteHelper.TABLE_PLACE, null,

		values);

		Cursor cursor = db.query(MySQLiteHelper.TABLE_PLACE,
				allColumns, MySQLiteHelper.COLUMN_ID + " = "
						+ insertId, null, null, null, null);

		cursor.moveToFirst();

		Place newTrackData = cursorToPlaceData(cursor);

		cursor.close();

		return newTrackData;

	}

	public void createPlace(ArrayList<Place> placeDatas) {
		if (placeDatas == null) {
			return;
		}
		System.out.println("Place to DB");
		for (int i = 0; i < placeDatas.size(); i++) {
			Place placeData = placeDatas.get(i);
			ContentValues values = new ContentValues();
			System.out.println(placeData.getLat());
			values.put(MySQLiteHelper.COLUMN_ID, placeData.getId());
			values.put(MySQLiteHelper.COLUMN_NAME, placeData.getName());
			values.put(MySQLiteHelper.COLUMN_DESCRIPTION, placeData.getDes());
			values.put(MySQLiteHelper.COLUMN_LAT, placeData.getLat());
			values.put(MySQLiteHelper.COLUMN_LNG, placeData.getLon());
			values.put(MySQLiteHelper.COLUMN_PIC, placeData.getPic());

			long insertId = db.insert(MySQLiteHelper.TABLE_PLACE, null,
					values);

		}

	}

	public void deletePlace(int id) {

		//long id = placeData.getId();

		System.out.println("Comment deleted with id: " + id);
		db.delete(MySQLiteHelper.TABLE_PLACE, MySQLiteHelper.COLUMN_ID
				+ " = " + id, null);

	}

	public ArrayList<Place> getAllPlace(int id) {

		ArrayList<Place> placeDatas = new ArrayList<Place>();
		Cursor cursor = db.query(MySQLiteHelper.TABLE_PLACE,
				allColumns, MySQLiteHelper.COLUMN_ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null);

		cursor.moveToFirst();

		while (!cursor.isAfterLast()) {

			Place placeData = cursorToPlaceData(cursor);

			placeDatas.add(placeData);
			cursor.moveToNext();
		}
		// make sure to close the cursor

		cursor.close();
		System.out.println(placeDatas.size());

		return placeDatas;

	}

	public ArrayList<Place> getAllRecord() {

		ArrayList<Place> recordDatas = new ArrayList<Place>();
		Cursor cursor = db.query(MySQLiteHelper.TABLE_PLACE,
				allColumns, null, null, null, null, null);

		cursor.moveToFirst();

		while (!cursor.isAfterLast()) {

			Place placeData = cursorToPlaceData(cursor);

			recordDatas.add(placeData);
			cursor.moveToNext();
		}
		// make sure to close the cursor

		cursor.close();

		return recordDatas;

	}
//
	private Place cursorToPlaceData(Cursor cursor) {
		Place placeData = new Place();
		placeData.setId(cursor.getInt(0));
		placeData.setName(cursor.getString(1));
		placeData.setDes(cursor.getString(2));
		placeData.setLat(cursor.getDouble(3));
		placeData.setLon(cursor.getDouble(4));
		placeData.setPic(cursor.getString(5));
		return placeData;

	}

}
